package com.wlanauth.web.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.SystemMenu;
import com.wlanauth.domain.manage.SystemResource;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.core.SystemConstants;
import com.wlanauth.web.core.tree.INode;
import com.wlanauth.web.core.tree.TreeNode;
import com.wlanauth.web.core.tree.TreeUtil;
import com.wlanauth.web.util.SimpleCacheManager;
/**
 * 系统菜单
 * @author zz
 * @date 2012-5-10
 * @email charles.zz.life@gmail.com
 */
public class SystemMenuAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(SystemMenuAction.class);
	
	/**
	 * 系统菜单树
	 * @return
	 */
	public String fetchManageMenuTree(){
		Set<SystemResource> resources = this.getCurrentOperatorResources();
		if(resources == null){
			this.jsonResult = new JsonResult(this,"该操作员无任何权限",false);
			return this.actionJsonResult();
		}  else {
			
			TreeNode root =getMenuTreeRootNode().cloneSelf();
			//过滤
			filter(root,null,resources);
			return this.actionJsonResult(root.getChildren());
		}
		
	}
	
	private void filter(TreeNode node,Iterator<TreeNode> parentNodeIt, Set<SystemResource> resources) {
		
		if(node.isLeaf()||node.getChildren()==null){ //如果是叶子节点
			for(SystemResource r:resources){
				if(node.getResourceCode()==null||node.getResourceCode().matches(r.getCode())){
					return ;
				}
			}
		} else {
			//非叶子节点，执行过滤
			Iterator<TreeNode> childrens = node.getChildren().iterator();
			while(childrens.hasNext()){
				TreeNode child = childrens.next();
				filter(child,childrens,resources);
			}
		}
		if((node.isLeaf()||node.getChildren().size()<1)&&parentNodeIt!=null){ //如果没有子节点，移除
			parentNodeIt.remove();
		}
	}

	protected static TreeNode getMenuTreeRootNode() {
		TreeNode root = (TreeNode) SimpleCacheManager.getInstance().get("com.wlanauth.web.action.SystemMenuAction.systemmenuTree");
		if(root == null){
			logger.info("开始 初始化系统菜单树.");
			GeneralCriteria criteria = new GeneralCriteria(SystemMenu.class);
			criteria.add(Restrictions.eq("available", true));
			criteria.addOrder(Order.asc("ordernum"));
		
			GeneralCriteriaResult<SystemMenu> result = criteria.executeQuery();
			
			root = toTreeRootNode(result.getData());
			
			SimpleCacheManager.getInstance().put("com.wlanauth.web.action.SystemMenuAction.systemmenuTree", root);
			logger.info("结束 初始化系统菜单树.");
		}
		return root;
	}
	
	private static TreeNode toTreeRootNode(List<SystemMenu> list){
		
		SystemMenu mRoot = new SystemMenu();
		mRoot.setId(SystemConstants.ROLE_ROOT);
		mRoot.setHref("#");
		mRoot.setName("根");
		mRoot.setParentId("");
		TreeNode root = TreeUtil.simpleToTree(mRoot,list,new INode(){
			public TreeNode entityToNode(Object obj){
				SystemMenu m = (SystemMenu)obj;
				TreeNode node = new TreeNode();
				node.setId(""+m.getId());
				node.setXtype(m.getHref());
				node.setText(m.getName());
				node.setParentId(m.getParentId());
				node.setIconCls(m.getIconCls());
				node.setResourceCode(m.getResourceCode());
				return node;
			}
		});
		return root;
	}
}
